<?php
namespace Com\Pay;
class WanYouFuPay{

    private $config = array();


    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/WanYouFuPayNotify';
//        $Sumprice = $price * 100;
        $order_sn = $order_info['ordernumber'];
        $nowtime = time();
        $post_data = array(
            'app_id' =>   intval($this->config['mch_id']),
            'pay_type'    =>  $order_info['pay_type']['type'],
            "order_id"  =>  $order_sn,
            'order_amt' => floatval($price),
            'notify_url'    => urlencode($notifyUrl),
            'return_url'    => urlencode($order_info['return_url']),
            'goods_name'    => urlencode("ChangbaoPay"),
            'time_stamp'    => $nowtime,
        );

        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
        $post_data['sign'] = $ContentSign; // Sign签名
        $tar_url = $order_info['pay_type']['send_url'];
//        var_dump($tar_url);die;
        $res = json_decode($this->https_request($tar_url,$post_data) , true);
//        var_dump($res);die;
        if($res['status_code'] == 0){
            return $res['pay_url'];
        }
        return '';
    }

    private function setParam($param,$key,$IS_Notify){
        if(!$IS_Notify) {
            $md5s = array(
                'app_id' => $param['app_id'],
                'pay_type' => $param['pay_type'],
                'order_id' => $param['order_id'],
                'order_amt' => $param['order_amt'],
                'notify_url' => urldecode($param['notify_url']),
                'return_url' => urldecode($param['return_url']),
                'time_stamp' => $param['time_stamp'],
            );
        }else{
            $md5s = array(
                'app_id' => $param['app_id'],
                'order_id' => $param['order_id'],
                'pay_seq' => $param['pay_seq'],
                'pay_amt' => $param['pay_amt'],
                'pay_result' => $param['pay_result'],
            );
        }
        $str = "";
        foreach ($md5s as $k => $v){
            $str.= $k . '=' . $v . '&';
        }
        $sign = md5($str . 'key=' .md5($key));
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['sign'] == $MySign){
            if($data['pay_result'] == '20'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


    private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
